package com.fable.sp.core.widgets;

import com.fable.sp.core.domain.WidgetInstance;
import com.fable.sp.core.service.GlobalPreferencesProvider;
import com.fable.sp.core.service.ValidationConfig;
import com.fable.sp.core.util.CollectionUtils;
import com.fable.sp.core.widget.ConfigurableWidget;
import com.fable.sp.core.widget.Preference;
import com.fable.sp.core.widget.WidgetRequest;
import com.fable.sp.core.widget.WidgetResponse;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * .
 *
 * @author stormning on 16/9/2.
 */
public abstract class SimpleWidget extends GenericWidget implements ConfigurableWidget, ValidationConfig {

    private String title;
    private String tags;
    private String info;
    private String cover;
    private List<Preference> preferences;
    @Autowired(required = false)
    private GlobalPreferencesProvider globalPreferencesProvider;

    @Override
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    @Override
    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    @Override
    public List<Preference> getPreferences() {
        if (globalPreferencesProvider == null) {
            return preferences;
        } else {
            boolean support = globalPreferencesProvider.support(this);
            if (support) {
                List<Preference> globalPrefs = globalPreferencesProvider.getPreferences();
                List<Preference> allPrefs = Lists.newArrayList();
                if (!CollectionUtils.isEmpty(globalPrefs)) {
                    allPrefs.addAll(globalPrefs);
                }
                if (!CollectionUtils.isEmpty(preferences)) {
                    allPrefs.addAll(preferences);
                }
                return allPrefs;
            }
            return preferences;
        }
    }

    public void setPreferences(List<Preference> preferences) {
        this.preferences = preferences;
    }


    @Override
    public void afterInstanceCreated(WidgetInstance instance, Long containerId) {

    }

    @Override
    public void beforeRender(WidgetRequest widgetRequest, WidgetResponse widgetResponse) {

    }

    @Override
    public void afterRender(WidgetRequest widgetRequest, WidgetResponse widgetResponse) {

    }

    @Override
    public void beforeSavePreference(WidgetInstance instance) {

    }

    @Override
    public void afterSavePreference(WidgetInstance instance) {

    }

    @Override
    public boolean useDefaultValidation() {
        return true;
    }
}
